message(NOTICE "开始构建 mysql_client 模块")
# 检查构建目标是否存在
if(TARGET mysql-client-${MYSQL_CLIENT_TAG})
    message(NOTICE "mysql client 模块已存在 mysql-client-${MYSQL_CLIENT_TAG}")
    return()
endif()
# 包含 模块依赖
include(${__DIR}/module/zlib_module.cmake)
include(${__DIR}/module/openssl_module.cmake)
include(${__DIR}/module/boost_module.cmake)
include(${__DIR}/module/ncurses_module.cmake)
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/mysql-client/${MYSQL_CLIENT_TAG}
    ${MODULE_INSTALL_DIR}/mysql-client/${MYSQL_CLIENT_TAG}
    mysql-client-${MYSQL_CLIENT_TAG})
# 构建 ssh 库
ExternalProject_Add(
    mysql-client-${MYSQL_CLIENT_TAG}
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/mysql-client/${MYSQL_CLIENT_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/mysql-client/${MYSQL_CLIENT_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/mysql-client/${MYSQL_CLIENT_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/mysql-client/${MYSQL_CLIENT_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/mysql-client/${MYSQL_CLIENT_TAG}          # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${MYSQL_CLIENT_REPO}
            -DGIT_TAG=${MYSQL_CLIENT_TAG}
            -P ${__DIR}/utils/git.cmake
    # ✅ 正确的 CMake 配置命令
    CONFIGURE_COMMAND
        ${CMAKE_COMMAND}
        -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DWITHOUT_SERVER=ON
        -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON
        -DDEFAULT_CHARSET=utf8mb4
        -DDEFAULT_COLLATION=utf8mb4_0900_ai_ci
        -DWITH_AUTHENTICATION_LDAP=OFF
        -DWITH_CLIENT_PROTOCOL_TRACING=ON
        -DWITH_AUTHENTICATION_KERBEROS=OFF
        -DWITH_FIDO=none
        -DWITH_SASL=OFF
        -DWITH_ZLIB=system
        -DWITH_LZ4=bundled
        -DWITH_ZSTD=bundled
        -DWITH_SSL=system
        -DDOWNLOAD_BOOST=0
        -DWITH_BOOST=${MODULE_SRC}/boost/${BOOST_TAG}
        -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
        -S <SOURCE_DIR>
        -B <BINARY_DIR>
    BUILD_COMMAND
        ${CMAKE_COMMAND}
            --build <BINARY_DIR>
            --target mysqlclient -j${NPROC}
    INSTALL_COMMAND
        ${CMAKE_COMMAND}
            --install <BINARY_DIR>
            --component Development
            --prefix <INSTALL_DIR>
            --config ${CMAKE_BUILD_TYPE}
        # 将安装文件路径保存到 文件去
        COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     OFF                           # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)


# 1 创建导入目标
add_library(mysql::client STATIC IMPORTED)
# ✅ 在 ExternalProject 后定义导入库
set_target_properties(mysql::client PROPERTIES
    IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/mysql/client/${MYSQL_CLIENT_TAG}/lib/libmysqlclient.a
    INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/mysql/client/${MYSQL_CLIENT_TAG}/include
    INTERFACE_LINK_LIBRARIES "ZLIB::ZLIBSTATIC;OpenSSL::SSL;OpenSSL::Crypto;-lresolv;-lm"
)

# 依赖
add_dependencies(
mysql-client-${MYSQL_CLIENT_TAG}
zlib-${ZLIB_TAG}
openssl-${OPENSSL_TAG}
ncurses-${NCURSES_TAG}
boost-${BOOST_TAG}
install-pkg-config
)
add_dependencies(mysql::client mysql-client-${MYSQL_CLIENT_TAG})
